<?php

class Testimonial_IndexController extends Core_Controller_Action_Standard
{
    public function init()
  {
  
    // @todo this may not work with some of the content stuff in here, double-check
    $subject = null;
    if( !Engine_Api::_()->core()->hasSubject() )
    {
      $id = $this->_getParam('id');
    
      /* TESTING ID HERE
    echo "m i here";
    echo $id;exit;   */
      // use viewer ID if not specified
      //if( is_null($id) )
      //  $id = Engine_Api::_()->user()->getViewer()->getIdentity();

      if( null !== $id )
      {
        $subject = Engine_Api::_()->user()->getUser($id);
        if( $subject->getIdentity() )
        {
          Engine_Api::_()->core()->setSubject($subject);
        }
      }
    }

   // $this->_helper->requireSubject('user');
    
  }
  public function indexAction()
  {
    //$this->view->someVar = 'someVal';
   if( !$this->_helper->requireUser()->isValid() ) return;
   
    $subject = Engine_Api::_()->core()->getSubject();
    $viewer = Engine_Api::_()->user()->getViewer();
     
      $this->view->form = $form = new Testimonial_Form_Create();
      
      if( $this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost()) ) {

      $table = Engine_Api::_()->getDbTable('testimonials','testimonial');
      $values = $form->getValues();
    //  $viewer = Engine_Api::_()->user()->getViewer();

      // Begin database transaction
      $db = $table->getAdapter();
      $db->beginTransaction();

      try
      {
        $testimonial = $table->createRow();
        $values['resource_type'] = $subject->getType();
        $values['resource_id'] = $subject->getIdentity();
        $values['sender_id'] = $viewer->getIdentity();
        $testimonial->setFromArray($values);
        $testimonial->save();
       
        // add notification
        Engine_Api::_()->getDbtable('notifications', 'activity')
          ->addNotification($subject, $viewer, $testimonial, 'request_testimonial');
        
        $db->commit();
      }

      catch( Exception $e )
      {
        $db->rollBack();
        throw $e;
      }
     
      /* This Code is for Redirectly On to the page with a POP _UP */
      return $this->_forward('success' ,'utility', 'core', array(
          'smoothboxClose' => true,
          'parentRefresh' => true,//Zend_Controller_Front::getInstance()->getRouter()->assemble(array('action' => 'manage'), 'group_general', true),
          'messages' => Array('Testomonial has been updated.')
        ));
    }
  
  }
  
  
// This Code is for Notification   
  public function requestTestiAction()
  {
    $this->view->notification = $notification = $this->_getParam('notification');
  }
  
  
  public function acceptAction()
  {
    $id = $this->_getParam('testimonial_id');  
    $n_id = $this->_getParam('notification_id');
    $testimonial = Engine_Api::_()->getItem('testimonial',$id);

    $db = Engine_Db_Table::getDefaultAdapter();
    $db->beginTransaction();
    try{
       $testimonial->approved = 1;
       $testimonial->save();
       
       // Set the requests as handled
      $notification = Engine_Api::_()->getDbtable('notifications', 'activity')
          ->getNotificationById($n_id);
      echo "<pre>";
      print_r($notification);
      if( $notification instanceof Activity_Model_Notification) {
        $notification->mitigated = 1;
        $notification->read = 1;
        $notification->save();
      }
       
       $db->commit();
    } 
    catch(Exception $e){
        $db->rollBack();

      $this->view->status = false;
      $this->view->error = Zend_Registry::get('Zend_Translate')->_('An error has occurred. ').$n_id.$id;
      $this->view->exception = $e->__toString();
    }
     
}
  public function rejectAction()
  {
  
  }
  public function viewAction() 
  {
    $viewer = Engine_Api::_()->user()->getViewer();
    
    // here $tId is written Because to find the row form a table ... and this is use to select row from table
    $tId = $this->_getParam('id');
    //echo $tId;exit;
        $table = Engine_Api::_()->getDbTable('testimonials','testimonial');
        $select = $table->select()->where('testimonial_id = ?',$tId);
        $this->view->testimonial = $testimonial = $table->fetchRow($select);
        
       //$this->view->form = $form = new Testimonial_Form_Reject();
  }
   
 
  public function confirmAction(){
        
      $viewer = Engine_Api::_()->user()->getViewer();
      
      $t_id = $this->_getParam('id');
      
      $db = Engine_Db_Table::getDefaultAdapter();
      $db->beginTransaction();
    
      $this->view->form = $form = new Testimonial_Form_Confirm();
      if( $this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost()) ) {
          // This Code is For Deletion for Item   
       try {
           $testimonial = Engine_Api::_()->getItem('testimonial',$t_id);
           
           $testimonial->approved = 1;
           $testimonial->save();
          
	        //activity feed
		    $api = Engine_Api::_()->getDbtable('actions', 'activity');
		    $action = $api->addActivity(Engine_Api::_()->getItem('user', $testimonial->sender_id), Engine_Api::_()->getItem('user', $testimonial->resource_id), 'testimonial_new');
				
		    // Commit
      		$db->commit();
           return $this->_forward('success' ,'utility', 'core', array(
                'smoothboxClose' => true,
                'parentRefresh' => true,//Zend_Controller_Front::getInstance()->getRouter()->assemble(array('action' => 'manage'), 'group_general', true),
                'messages' => Array('Your Testimonial has been Confirmed')
                ));
           
       } catch(Exception $e){
       	  $db->rollBack();
           throw $e;
       }
      }
  }
  
  
  public function removeAction(){
        
      $viewer = Engine_Api::_()->user()->getViewer();
      
      $t_id = $this->_getParam('id');
    
      $this->view->form = $form = new Testimonial_Form_Delete();
      
      if( $this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost()) ) {
          // This Code is For Deletion for Item   
       try {
           $testimonial = Engine_Api::_()->getItem('testimonial',$t_id);
           //$testimonial->approved = 1;
           $testimonial->delete();
                           
           return $this->_forward('success' ,'utility', 'core', array(
                'smoothboxClose' => true,
                'parentRedirect' => Zend_Controller_Front::getInstance()->getRouter()->assemble(array(), 'home', true),
                'messages' => Array('Successfully Removed')
                ));
           
       } catch(Exception $e){
           throw $e;
       }
      }
  }
      
public function browseAction()
  {
  	$items_per_page =  20;
    // Prepare data
     $table = Engine_Api::_()->getDbtable('testimonials','testimonial');
    
    // Get paginator
    $this->view->paginator = $paginator = Zend_Paginator::factory($table->select()->where('resource_id = ?', $this->_getParam('id'))->where('approved = 1'));
    $paginator->setItemCountPerPage($items_per_page);
    $this->view->paginator = $paginator->setCurrentPageNumber( $this->_getParam('page') );

  }
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 /*  public function deleteAction()
  {
          
    $viewer = Engine_Api::_()->user()->getViewer();
    
    $id = $this->_getParam('testimonial_id');
    $testimonial = Engine_Api::_()->getItem('testimonial',$id);
    
    
    //$testimonial = Engine_Api::_()->getItem('testimonials', $this->getRequest()->getParam('testimonial_id'));
    //if( !$this->_helper->requireAuth()->setAuthParams($testimonial, null, 'delete')->isValid()) return;

    // In smoothbox
    $this->_helper->layout->setLayout('default-simple');

    $this->view->form = $form = new Testimonial_Form_Delete();

    if( !$testimonial ) {
      $this->view->status = false;
      $this->view->error = Zend_Registry::get('Zend_Translate')->_("Testimonial entry doesn't exist or not authorized to delete");
      return;
    }

    if( !$this->getRequest()->isPost() ) {
      $this->view->status = false;
      $this->view->error = Zend_Registry::get('Zend_Translate')->_('Invalid request method');
      return;
    }

    $db = $testimonial->getTable()->getAdapter();
    $db->beginTransaction();

    try {
     $testimonial->approved = 1;
      $testimonials->delete();
      
      $db->commit();
    } catch( Exception $e ) {
      $db->rollBack();
      throw $e;
    }

    $this->view->status = true;
    $this->view->message = Zend_Registry::get('Zend_Translate')->_('Your Testimonial entry has been deleted.');
    return $this->_forward('success' ,'utility', 'core', array(
      'parentRedirect' => Zend_Controller_Front::getInstance()->getRouter()->assemble(array('action' => 'view'), 'testimonial_general', true),
      'messages' => Array($this->view->message)
    ));
  }*/
 
 

}